﻿package core.drawing
{
    import core.enum.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    final public class SharpenTool extends ShapeTool
    {
        var soffset:Point;
        var source:BitmapData;
        var px:uint;
        var pt:uint;
        var f:Number;
        var tf:Number;
        var amt:int;
        var pxl:uint;
        var pxc:uint;
        var pxr:uint;
        var pxt:uint;
        var pxb:uint;
        var n:int;
        var cx:int;
        var cy:int;
        var sx:int;
        var sy:int;
        var alpha:int;
        var a:int;
        var r:int;
        var g:int;
        var b:int;
        var ta:int;
        var tr:int;
        var tg:int;
        var tb:int;

        public function SharpenTool(param1:Tuyitu)
        {
            super(param1, ToolType.Sharpen);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            if (!type.attitude.locked)
            {
                this.UpdateReferences();
                this.Prepare(Action.SharpenTool, layer.SetBitmapEditMode(options.strength, canvas.clip));
                this.source = layer.bitmap.bitmapData;
                this.soffset = new Point(layer.x, layer.y);
                if (!event.shiftKey)
                {
                    var _loc_2:int = -1;
                    y = -1;
                    x = _loc_2;
                }
                this.MouseDrag(null);
            }
            return;
        }// end function

        override public function DrawShape(canvas:int, canvas:int) : void
        {
            if (this.source != null)
            {
                this.n = 0;
                this.sy = canvas - int(shape.height * 0.5);
                this.cy = this.sy + shape.height;
                while (this.sy < this.cy)
                {
                    
                    this.sx = canvas - int(shape.width * 0.5);
                    this.cx = this.sx + shape.width;
                    while (this.sx < this.cx)
                    {
                        
                        this.amt = 0;
                        this.alpha = shape.data[this.n];
                        if (this.alpha > 0 && (selection == null || selection.IsSelected(this.sx + offset.x, this.sy + offset.y)))
                        {
                            this.amt = 0;
                            this.a = 0;
                            this.r = 0;
                            this.g = 0;
                            this.b = 0;
                            this.pxt = this.source.getPixel32(this.sx + offset.x - this.soffset.x, this.sy + offset.y - this.soffset.y - 1);
                            this.pxb = this.source.getPixel32(this.sx + offset.x - this.soffset.x, this.sy + offset.y - this.soffset.y + 1);
                            this.pxc = this.source.getPixel32(this.sx + offset.x - this.soffset.x, this.sy + offset.y - this.soffset.y);
                            this.pxl = this.source.getPixel32(this.sx + offset.x - this.soffset.x - 1, this.sy + offset.y - this.soffset.y);
                            this.pxr = this.source.getPixel32(this.sx + offset.x - this.soffset.x + 1, this.sy + offset.y - this.soffset.y);
                            if (this.pxl >>> 24 & 255 > 0)
                            {
                                this.a = this.a + (this.pxl >>> 24 & 255);
                                this.r = this.r + (this.pxl >>> 16 & 255);
                                this.g = this.g + (this.pxl >>> 8 & 255);
                                this.b = this.b + (this.pxl & 255);
                                var _loc_3:String = this;
                                var _loc_4:* = this.amt + 1;
                                _loc_3.amt = _loc_4;
                            }
                            if (this.pxr >>> 24 & 255 > 0)
                            {
                                this.a = this.a + (this.pxr >>> 24 & 255);
                                this.r = this.r + (this.pxr >>> 16 & 255);
                                this.g = this.g + (this.pxr >>> 8 & 255);
                                this.b = this.b + (this.pxr & 255);
                                var _loc_3:String = this;
                                var _loc_4:* = this.amt + 1;
                                _loc_3.amt = _loc_4;
                            }
                            if (this.pxc >>> 24 & 255 > 0)
                            {
                                this.a = this.a + (this.pxc >>> 24 & 255);
                                this.r = this.r + (this.pxc >>> 16 & 255);
                                this.g = this.g + (this.pxc >>> 8 & 255);
                                this.b = this.b + (this.pxc & 255);
                                var _loc_3:String = this;
                                var _loc_4:* = this.amt + 1;
                                _loc_3.amt = _loc_4;
                            }
                            if (this.pxt >>> 24 & 255 > 0)
                            {
                                this.a = this.a + (this.pxt >>> 24 & 255);
                                this.r = this.r + (this.pxt >>> 16 & 255);
                                this.g = this.g + (this.pxt >>> 8 & 255);
                                this.b = this.b + (this.pxt & 255);
                                var _loc_3:String = this;
                                var _loc_4:* = this.amt + 1;
                                _loc_3.amt = _loc_4;
                            }
                            if (this.pxb >>> 24 & 255 > 0)
                            {
                                this.a = this.a + (this.pxc >>> 24 & 255);
                                this.r = this.r + (this.pxc >>> 16 & 255);
                                this.g = this.g + (this.pxc >>> 8 & 255);
                                this.b = this.b + (this.pxc & 255);
                                var _loc_3:String = this;
                                var _loc_4:* = this.amt + 1;
                                _loc_3.amt = _loc_4;
                            }
                            this.a = this.a / 5;
                            this.r = this.r / this.amt;
                            this.g = this.g / this.amt;
                            this.b = this.b / this.amt;
                            this.ta = this.pxc >>> 24 & 255;
                            this.tr = this.pxc >>> 16 & 255;
                            this.tg = this.pxc >>> 8 & 255;
                            this.tb = this.pxc & 255;
                            this.ta = this.ta + (this.ta - this.a);
                            this.tr = this.tr + (this.tr - this.r);
                            this.tg = this.tg + (this.tg - this.g);
                            this.tb = this.tb + (this.tb - this.b);
                            if (this.ta > 255)
                            {
                                this.ta = 255;
                            }
                            else if (this.ta < 0)
                            {
                                this.ta = 0;
                            }
                            if (this.tr > 255)
                            {
                                this.tr = 255;
                            }
                            else if (this.tr < 0)
                            {
                                this.tr = 0;
                            }
                            if (this.tg > 255)
                            {
                                this.tg = 255;
                            }
                            else if (this.tg < 0)
                            {
                                this.tg = 0;
                            }
                            if (this.tb > 255)
                            {
                                this.tb = 255;
                            }
                            else if (this.tb < 0)
                            {
                                this.tb = 0;
                            }
                            if (this.ta == 255 && this.alpha < 255)
                            {
                                this.pxc = target.getPixel32(this.sx, this.sy);
                                if (this.pxc >>> 24 & 255 > 0)
                                {
                                    this.r = this.pxc >>> 16 & 255;
                                    this.g = this.pxc >>> 8 & 255;
                                    this.b = this.pxc & 255;
                                    this.f = this.alpha / 255;
                                    this.tf = 1 - this.f;
                                    this.tr = this.tr * this.f + this.r * this.tf;
                                    this.tg = this.tg * this.f + this.g * this.tf;
                                    this.tb = this.tb * this.f + this.b * this.tf;
                                }
                            }
                            target.setPixel32(this.sx, this.sy, this.ta << 24 | this.tr << 16 | this.tg << 8 | this.tb);
                        }
                        var _loc_3:String = this;
                        var _loc_4:* = this.n + 1;
                        _loc_3.n = _loc_4;
                        var _loc_3:String = this;
                        var _loc_4:* = this.sx + 1;
                        _loc_3.sx = _loc_4;
                    }
                    var _loc_3:String = this;
                    var _loc_4:* = this.sy + 1;
                    _loc_3.sy = _loc_4;
                }
            }
            return;
        }// end function

    }
}
